package cn.qiao.demo.util;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 一个异步线程的工具类
 *
 * @author callme-qiao
 */
public class AsynUtil {

    private static BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(30);
    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(4, 6, 60, TimeUnit.SECONDS, queue);

    /**
     * 执行
     */
    public static void execute(String s) {
        threadPoolExecutor.execute(new DoWork(s));
    }



}
class DoWork implements Runnable{


    private List<String> list = new ArrayList<>();

    public DoWork(String s) {
       list.add(s);
    }
    @Override
    public void run() {
        for (String s : list) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程执行中"+s);
        }
    }
}